package com.ganxiang.ximenhong.widght;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

public class LocationPopup extends PopupWindow {

	private Context mContext;
	public View mView;
	public PopupDissmiss mPopupDissmis;
	private int mViewId;
	public int mViewHight;
	public int mViewWidht;

	public LocationPopup(Context context, int layoutId) {
		super();
		this.mContext = context;
		this.mViewId = layoutId;
		setTouchable(true);
		setFocusable(true);
		setOutsideTouchable(true);
		setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());

		this.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				if (mPopupDissmis != null && LocationPopup.this.isShowing()) {
					mPopupDissmis.onPopupDissmiss();
				}
			}
		});
		
		mView = View.inflate(mContext, mViewId, null);
//		mView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//		mViewHight = mView.getMeasuredHeight();
//		mViewWidht = mView.getMeasuredWidth();
		setContentView(mView);
//		ViewTreeObserver vto = mView.getViewTreeObserver();    
//		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
//		    @Override    
//		    public void onGlobalLayout() { 
//		    	mViewHight = mView.getMeasuredHeight();
//		    	mViewWidht = mView.getMeasuredWidth();	      
//		    }    
//		});    
//		int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
//		int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
//		mView.measure(w, h);  
//		mViewHight =mView.getMeasuredHeight();  
//		mViewWidht =mView.getMeasuredWidth();  
	}

	
	
	public void setmPopupDissmis(PopupDissmiss mPopupDissmis) {
		this.mPopupDissmis = mPopupDissmis;
	}

	public void show(View v, int x, int y) {
		if (this != null && !this.isShowing()) {
			showAtLocation(v, Gravity.NO_GRAVITY, x, y);
		}
	}

	public void show(View v){
		showAsDropDown(v);
	}
	
	public interface PopupDissmiss {
		void onPopupDissmiss();
	}

}
